﻿function AutomateWPS(tableid,letter)
{
    try
    {
        //        var curTbl = document.getElementById(tableid);
        var oXL = new ActiveXObject("ET.Application"); 
        if (oXL == null)
        {
            alert('导出WPS失败，确定是否安装了WPS，浏览器设置是否正确');
        }else{
            //创建AX对象excel 
            var oWB = oXL.Workbooks.Add(); 
            //获取workbook对象 
            var oSheet = oWB.ActiveSheet; 
            //激活当前sheet 
            var Lenr =tableid.rows.length;// curTbl.rows.length;
            //取得表格行数 
            var merge='';
            merge ='A1:'+letter+'1';
            oXL.Range(merge).MergeCells = true;  //合并单元格
            merge ='A2:'+letter+'2';
            oXL.Range(merge).MergeCells = true;  //合并单元格
            
            oSheet.Columns("A:A").ColumnWidth = 20;            
            oSheet.Columns("B:B").ColumnWidth = 5;            
            oSheet.Columns("C:C").ColumnWidth = 10;
            oSheet.Columns("D:D").ColumnWidth = 10;
            oSheet.Columns("E:E").ColumnWidth = 10;
            oSheet.Columns("F:F").ColumnWidth = 10;
            
            oSheet.Cells(1,1).HorizontalAlignment = 3;
            oSheet.Rows("1:1").Font.Size = 18;   //设置第1行的字体大小
            oSheet.Rows("1:1").Font.Bold = true;  //设置第1，1行为粗体
            oSheet.Columns("A:A").ColumnWidth = 30;  //设置单元格宽度
            
            for (i = 0; i < Lenr; i++) 
            { 
                var Lenc = tableid.rows(i).cells.length; 
                //取得每行的列数 
                for (j = 0; j < Lenc; j++) 
                {
                    var cellValue;
                    cellValue = tableid.rows(i).cells(j).innerText;
                    if(j==1 || j==2)
                    {
                        cellValue = "'"+cellValue; //转换为文本格式
                    }  
                    oSheet.Cells(i + 1, j + 1).value = cellValue; 
                } 
            } 
            //设置excel可见属性
            oXL.Visible = true;
        }
    }catch(e){
        alert("导出WPS失败，确定是否安装了WPS");

        oXL = null; 
        oWB = null; 
        oSheet = null;
    }
    //oSheet.Columns.AutoFit; 
    //oXL.UserControl = true;  //wps交由用户控制 很重要
}